// pages/order-pages/order-list/index.js
import { ORDERLIST_TABS, ORDER_TYPE_ENUM } from '../../../constants/order';
import { OrderListMixins } from '../../../behaviors/page';
import { getExpandOrderList, getExpandAfterSaleList } from '../../api/order';
import { PACKAGE_ORDER_PAGE } from '../../../constants/route';
const { navTo, showLoading, hideLoading } = require('../../../common/env');
Page({
    mixins: [OrderListMixins],
    data: {
        tabsData: ORDERLIST_TABS,
        orderTypesEnum: ORDER_TYPE_ENUM,
        orderList: [],
        afterSaleList: [],
        searchTerm: '',
        searchValue: '',
        isHaveFilter: true,
    },
    onLoad: function (options) {
        const { filterType='', filterValue='' } = options;
        this.setData({
            searchTerm: filterType,
            searchValue: filterValue,
            isHaveFilter: filterType === '',
        }, async () => {
            await this.getOrderListData(true);
        });
    },
    // 获取订单列表
    getOrderListData: async function (isRefresh = false) {
        showLoading('加载中...');
        const { activeIndex, orderList, afterSaleList, pageSize, pageNo, pageTime, searchTerm, searchValue } = this.data;
        let param = { pageSize, pageNo, pageTime };
        if (searchValue) param = { ...param, searchTerm, searchValue };
        if (activeIndex === ORDER_TYPE_ENUM.afterSale) {
            // 售后
            const afterList = await getExpandAfterSaleList(param);
            console.log('ExpandOrderList => 获取售后订单数据为：', afterList, isRefresh);
            this.setData({
                afterSaleList: isRefresh ? afterList : [...afterSaleList, ...afterList],
            });
        } else {
            const isAll = activeIndex === ORDER_TYPE_ENUM.all;
            if (isAll) {
                param.isAll = true;
            } else {
                param.orderStatus = ORDERLIST_TABS.filter(item => item.name === activeIndex)[0].value;
            }
            // 正常单
            const list = await getExpandOrderList(param);
            console.log('ExpandOrderList => 获取订单数据为：', list, isRefresh);
            this.setData({
                orderList: isRefresh ? list : [...orderList, ...list],
            });
        }
        hideLoading();
    },
    // 订单详情
    invokeDetailAction: function (event) {
        const { serialNumber, spokesmanId } = event.detail;
        navTo(PACKAGE_ORDER_PAGE.expandOrderDetail, {
            orderNumber: serialNumber,
            spnId: spokesmanId,
        });
    },
    // 售后单详情
    invokeAfterSaleDetailAction: function (event) {
        const { serialNumber, spokesmanId } = event.detail;
        navTo(PACKAGE_ORDER_PAGE.expandAfterSaleDetail, {
            orderNumber: serialNumber,
            spnId: spokesmanId,
        });
    },
    // 处理查询条件变化
    invokeFilterAction: function (event) {
        const { type, value } = event.detail;
        // 保存筛选条件
        this.setData({
            pageNo: 1,
            searchTerm: type,
            searchValue: value,
        }, async () => await this.getOrderListData(true));
    },
})